package com.solidsnake.scala.service

import java.util.Date
import java.text.SimpleDateFormat

/**
 * Created with IntelliJ IDEA.
 * User: Solidsnake
 * Date: 13-12-2
 * Time: 下午11:56
 * To change this template use File | Settings | File Templates.
 */
class CompanyInfo {

  /**
   * 名称
   */
  var name : String = _
  /**
   * 状态
   */
  var status : String = _
  /**
   * 公司类型
   */
  var companyType : String = _
  /**
   * 行为
   */
  var action : String = _
  /**
   * 时间
   */
  var time : Date = _
}

object CompanyInfo {
  def apply(name : String, status : String, companyType : String, action : String, time : String) : CompanyInfo= {
    val target = new CompanyInfo()
    target.name = name.trim
    target.status = status.trim
    target.companyType = companyType.trim
    target.action = action.trim
    target.time = new SimpleDateFormat("yyyy-MM-dd").parse(time.trim)
    target
  }


  def regexCompanyGroup(context : String) = {
    var companyInfoLists : List[CompanyInfo] = Nil
    val groupRegex = "<tr>[^<]*<td[^>]*style=\"width: 300px;\"[^>]*>[^r]*</tr>".r
    val companyGroup = groupRegex.findAllIn(context)
    val attributeRegex = "<td[^>]*>([^<]*)</td>".r

    while(companyGroup.hasNext) {
      val singleCompany = companyGroup.next()
      val attributes = attributeRegex.findAllIn(singleCompany)
      val attributeList = for(index <- 1 to 5) yield {
        val attributeRegex(attribute) = attributes.next()
        attribute
      }
      companyInfoLists = CompanyInfo(attributeList(0), attributeList(1), attributeList(2), attributeList(3), attributeList(4)) :: companyInfoLists
    }
    companyInfoLists
  }
}
